package com.io;

import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

/**
 * Created by yangliwei on 2017/7/23.
 */
public class FileDemo {


    /**
     * 输出规范路径名
     */
    @Test
    public void testGetCanonicalPath() {
        String path = System.getProperty("user.dir") + File.separator + "webroot";
        File file = new File(path);
        try {
            String fileCanonicalPath = file.getCanonicalPath();
            System.out.println(fileCanonicalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 还是返回当前项目的根目录
     */
    @Test
    public void testFile() {
        File file = new File("");
//        D:\ideaproject\javastudy
        System.out.println(file.getAbsolutePath());
    }

    //新建一个文件
    @Test
    public void testFile1() {
        File file = new File("D://newfile");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 根据文件创建时间批量修改文件
     *
     * @throws InterruptedException
     */
    @Test
    public void testBathModifyFileOrderByCreateDate() throws InterruptedException {
        Thread.sleep(10 * 1000);
        File dir = new File("E:\\tmp\\test");
        if (!dir.isDirectory()) {
            System.out.println("请输入目录");
            return;
        }
        File[] files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.isFile();
            }
        });
        List<File> fileList = Arrays.asList(files);

        fileList.sort((f1, f2) -> {
            if (f1.lastModified() > f2.lastModified()) {
                return 1;
            } else {
                return -1;
            }

        });
        System.out.println(fileList);

        for (int i = 0; i < fileList.size(); i++) {

            String fileNameExt = fileList.get(0).getName().split("\\.")[1];
            File tmp = new File(dir + File.separator + i + "." + fileNameExt);

            fileList.get(i).renameTo(tmp);
        }

    }

    /**
     * 比较两个文件信息，将相同的输出
     */
    @Test
    public void compare2File() {
        String patha = "E:\\tmp\\b2itmall.csv";
        String pathb = "E:\\tmp\\wttmall.csv";
        List<String> aStrings = readFile2Cache(patha);
        List<String> bStrings = readFile2Cache(pathb);
        for (int i = 0; i < aStrings.size(); i++) {
            for (int j = 0; j < bStrings.size(); j++) {
                if (aStrings.get(i).equals(bStrings.get(j))) {
                    System.out.println("aFile line " + (i + 1) + " ; bFile line b " + (j + 1) + " String " + aStrings.get(i));
                }
            }
        }
    }

    public List<String> readFile2Cache(String filePath) {

        List<String> result = new ArrayList<>();
        try (LineNumberReader reader = new LineNumberReader(new FileReader(filePath));) {

            String line = null;
            while ((line = reader.readLine()) != null) {
                result.add(line);
            }
            reader.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
